package com.fwtai.service.core;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.ConfigFile;
import com.fwtai.config.LocalUserId;
import com.fwtai.core.RoleDao;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * 角色管理
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2020-03-13 17:24
 * @QQ号码 444141300
 * @Email service@dwlai.com
 * @官网 http://www.fwtai.com
*/
@Service
public class RoleService{

  @Resource
  private RoleDao roleDao;

  public String add(final PageFormData formData){
    final String p_role_name = "role_name";
    final String p_role_flag = "role_flag";
    final String validate = ToolClient.validateField(formData,p_role_name,p_role_flag);
    if(validate != null)return validate;
    final String role_name = formData.getString(p_role_name);
    final String role_flag = formData.getString(p_role_flag);
    final String exist_role_name = roleDao.queryRoleName(role_name);
    if(exist_role_name != null){
      return ToolClient.createJsonFail("角色名称<br/><span style='color:#f00'>"+role_name+"</span><br/>已存在,请换一个");
    }
    final String exist_role_flag = roleDao.queryRoleFlag(role_flag);
    if(exist_role_flag != null){
      return ToolClient.createJsonFail("角色标识<br/><span style='color:#f00'>"+role_flag+"</span><br/>已存在,请换一个");
    }
    formData.put("kid",new IdBuilder().getId121L());
    final String upper = role_flag.toUpperCase();
    if(!upper.startsWith("ROLE_")){
      return ToolClient.createJsonFail("角色标识必须以role_开头!");
    }
    formData.put("role_flag",upper);
    return ToolClient.executeRows(roleDao.add(formData));
  }

  public String edit(final PageFormData formData){
    final String p_role_name = "role_name";
    final String p_role_flag = "role_flag";
    final String p_kid = "kid";
    final String validate = ToolClient.validateField(formData,p_role_name,p_role_flag,p_kid);
    if(validate != null)return validate;
    final String role_name = formData.getString(p_role_name);
    final String role_flag = formData.getString(p_role_flag);
    final String kid = formData.getString(p_kid);
    final String exist_role_name = roleDao.queryRoleName(role_name);
    if(exist_role_name != null){
      if(!exist_role_name.equals(kid)){
        return ToolClient.createJsonFail("角色名称<br/><span style='color:#f00'>"+role_name+"</span><br/>已存在,请换一个");
      }
    }
    final String exist_role_flag = roleDao.queryRoleFlag(role_flag);
    if(exist_role_flag != null){
      if(!exist_role_flag.equals(kid)){
        return ToolClient.createJsonFail("角色标识<br/><span style='color:#f00'>"+role_flag+"</span><br/>已存在,请换一个");
      }
    }
    final String upper = role_flag.toUpperCase();
    if(!upper.startsWith("ROLE_")){
      return ToolClient.createJsonFail("角色标识必须以role_开头!");
    }
    formData.put("role_flag",upper);
    return ToolClient.executeRows(roleDao.edit(formData));
  }

  public String delById(final PageFormData formData){
    final String p_kid = "id";
    final String validate = ToolClient.validateField(formData,p_kid);
    if(validate != null)return validate;
    final String kid = formData.getString(p_kid);
    if(ConfigFile.SUPER_KEY.equals(kid)){
      return ToolClient.jsonValidateField();
    }
    final String exist_key_kid = roleDao.queryExistById(kid);
    if(exist_key_kid == null){
      return ToolClient.createJsonFail("角色已不存在");
    }
    return ToolClient.executeRows(roleDao.delById(kid));
  }

  public String delByKeys(final PageFormData formData){
    final String p_ids = "ids";
    final String validate = ToolClient.validateField(formData,p_ids);
    if(validate != null)return validate;
    final String ids = formData.getString(p_ids);
    if(ids.contains(ConfigFile.SUPER_KEY)){
      return ToolClient.jsonValidateField();
    }
    final ArrayList<String> lists = ToolString.keysToList(ids);
    if(lists == null || lists.size() <= 0){
      return ToolClient.createJsonFail("请选择要删除的数据");
    }
    return ToolClient.executeRows(roleDao.delByKeys(lists),"操作成功","角色已不存在,刷新重试");
  }

  public String listData(PageFormData formData){
    try {
      formData = ToolClient.dataTableMysql(formData);
      if(formData == null)return ToolClient.jsonValidateField();
      final String userId = LocalUserId.get();
      if(ConfigFile.SUPER_KEY.equals(userId)){
        formData.put("keySuper",userId);
      }
      final HashMap<String,Object> map = roleDao.listData(formData);
      return ToolClient.dataTableOK((List<Object>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
    } catch (Exception e){
      return ToolClient.dataTableException(formData.get("sEcho"));
    }
  }

  public String delEmptyMenu(final PageFormData formData){
    final String p_kid = "kid";
    final String validate = ToolClient.validateField(formData,p_kid);
    if(validate != null)return validate;
    final String roleId = formData.getString(p_kid);
    return ToolClient.executeRows(roleDao.delEmptyMenu(roleId));
  }

  //根据指定roleId获取角色菜单
  public String getRoleMenu(final PageFormData formData){
    final String p_roleId = "roleId";
    final String validate = ToolClient.validateField(formData,p_roleId);
    if(validate != null)return validate;
    final String userId = LocalUserId.get();
    if(ConfigFile.SUPER_KEY.equals(userId)){
      return ToolClient.queryJson(roleDao.getRoleMenuSuper(formData.getString(p_roleId)));
    }else{
      formData.put("userId",userId);
      return ToolClient.queryJson(roleDao.getRoleMenu(formData));
    }
  }

  public String saveRoleMenu(final PageFormData formData){
    final String p_roleId = "roleId";
    final String validate = ToolClient.validateField(formData,p_roleId);
    if(validate != null)return validate;
    final String kids = formData.getString("kids");
    final String roleId = formData.getString(p_roleId);
    final ArrayList<String> listMenus = ToolString.keysToList(kids);
    if(listMenus == null || listMenus.size() <= 0){
      final int rows = roleDao.delEmptyMenu(roleId);
      return ToolClient.executeRows(rows,"操作成功","未做任何操作,因为暂无私有菜单");
    }else{
      final ArrayList<HashMap<String,String>> listMaps = new ArrayList<HashMap<String,String>>();
      final Iterator<String> iterator = listMenus.iterator();
      while(iterator.hasNext()){
        final HashMap<String,String> map = new HashMap<String,String>();
        map.put("menu_id",iterator.next());
        map.put("role_id",roleId);
        listMaps.add(map);
      }
      final int rows = roleDao.saveRoleMenu(roleId,listMaps);
      return ToolClient.executeRows(rows);
    }
  }
}